package com.example.boxueguapplication;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.boxueguapplication.model.ChapterTitle;
import com.thoughtworks.xstream.XStream;

import java.io.InputStream;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link XitiFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class XitiFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public XitiFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment XitiFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static XitiFragment newInstance(String param1, String param2) {
        XitiFragment fragment = new XitiFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_xiti, container, false);
    }

    private RelativeLayout appBar;
    private ImageView back;
    private TextView titleBar;
    private RecyclerView mxiti_box;
    private List<ChapterTitle.Course> XiTiTitle_data;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findView(view);
        back.setVisibility(View.GONE);
        titleBar.setText("博学谷习题");
        InputStream in = getResources().openRawResource(R.raw.chaptertitle);
        XStream xstream = new XStream();
        xstream.processAnnotations(ChapterTitle.class);
        ChapterTitle chapterTitle = (ChapterTitle) xstream.fromXML(in);
        XiTiTitle_data = chapterTitle.xiTiCourse;
        mxiti_box.setLayoutManager(new LinearLayoutManager(getActivity()));
        XiTiTitleAdapter xiTiTitleAdapter = new XiTiTitleAdapter(R.layout.xiti_title_item, XiTiTitle_data);
        xiTiTitleAdapter.setOnItemClickListener((adapter, view1, position) -> {
            List data = adapter.getData();
            Intent intent = new Intent(getContext(), XiTiActivity.class);
            ChapterTitle.Course course = (ChapterTitle.Course) data.get(position);
            intent.putExtra("id", course.id);
            intent.putExtra("title",course.title);
            startActivity(intent);
        });
        mxiti_box.setAdapter(xiTiTitleAdapter);
    }

    private void findView(View view) {
        appBar = view.findViewById(R.id.app_bar);
        back = (ImageView) view.findViewById(R.id.back);
        titleBar = (TextView) view.findViewById(R.id.title_bar);
        mxiti_box = view.findViewById(R.id.xiti_list);
    }

    public class XiTiTitleAdapter extends BaseQuickAdapter<ChapterTitle.Course, BaseViewHolder> {

        public XiTiTitleAdapter(int layoutResId, List<ChapterTitle.Course> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, ChapterTitle.Course course) {
            holder.setText(R.id.xiti_title, course.title);
            switch (holder.getAdapterPosition() % 4) {
                case 0:
                    holder.setBackgroundResource(R.id.title_id, R.drawable.exercises_bg_1);
                    break;
                case 1:
                    holder.setBackgroundResource(R.id.title_id, R.drawable.exercises_bg_2);
                    break;
                case 2:
                    holder.setBackgroundResource(R.id.title_id, R.drawable.exercises_bg_3);
                    break;
                case 3:
                    holder.setBackgroundResource(R.id.title_id, R.drawable.exercises_bg_4);
                    break;
                default:
                    break;
            }
            holder.setText(R.id.title_id,course.id+"");
            holder.setText(R.id.xiti_size,"共计5题");

        }
    }
}